import { createMessage } from './tool/index.js'
import { getType } from './tool/tool'
import { baseURL } from '@/plugins/config.js'

// 自定义插件
import * as filters from './filters'
import * as directives from './directives'



import klMessage from './components/kl-message/index.vue'
import klLoading from '@/mixins/components/kl-loading/index.vue'


import APIS from '@/api/common.js'


// 创建方式
// 也可以传递参数，给props
const klmessage = (params) => {
    let type = getType(params)
    let message = ''
    let sleep = 3000
    if (type === 'string') {
        message = params
    } else if (type === 'object') {
        message = params.message || ''
        sleep = params.sleep || 3000
    }

    return createMessage(klMessage, { message, sleep })
}

// 全局loading
const loading = (params) => {
    let type = getType(params)
    let result = {}
    if (type === 'string') {
        result.msg = params
    } else if (type === 'object') {
        result = params
    }
    return createMessage(klLoading, result)
}



// 批量注册
let componentsRegister = import.meta.glob('./components/defaultRegister/*.vue')

// vComponents 对vant二次封装的组件库
let vComponents = import.meta.glob('./components/aComponents/*.vue')

componentsRegister = { ...componentsRegister, ...vComponents }

export default {
    install(Vue) {
        // 注册全局过滤器
        Object.keys(filters).forEach((key) => {
            Vue.filter(key, filters[key])
        })

        // 注册全局指令
        Object.keys(directives).forEach((k) => Vue.directive(k, directives[k]))

        // 批量注册一个文件夹下的组件
        for (const [key, value] of Object.entries(componentsRegister)) {
            // 这儿的name是文件名
            const name = key.slice(key.lastIndexOf('/') + 1, key.lastIndexOf('.'))
            Vue.component(name, value)
        }


        // 向全局上挂载一个方法
        Vue.prototype.$klMessage = klmessage
        Vue.prototype.$klLoading = loading

        // 全局挂载变量
        Vue.prototype.isProd = process.env.NODE_ENV !== 'development'
        Vue.prototype.emojiBasePath =
            'https://image.zlketang.com/public/news/others/micro_group/emoji/'

        Vue.prototype.$apis = APIS
        Vue.prototype.baseURL = baseURL

        // Vue.use(vant.Lazyload)

        // 混入一个function 专门用来处理每页的 headers信息
        Vue.mixin({
            methods: {
                $seo(title, content, payload = []) {
                    return {
                        title,
                        meta: [
                            {
                                hid: 'descrition',
                                name: 'keywords',
                                content,
                            },
                            ...payload,
                        ],
                    }
                },
            },
        })

        // 重写elementUi的message方法 由于elementUi使用了缓存导入，所以需要使用延时
        // let timer = setInterval(() => {
        //     if (ELEMENT) {
        //         clearInterval(timer)
        //         timer = null
        //         let messageInstance = null
        //         const overrideMessage = (options) => {
        //             if (messageInstance) {
        //                 messageInstance.close()
        //             }
        //             messageInstance = ELEMENT.Message(options)
        //         }

        //         ['error', 'success', 'info', 'warning'].forEach((type) => {
        //             ELEMENT.Message[type] = (options) => {
        //                 if (typeof options === 'string') {
        //                     options = {
        //                         message: options,
        //                     }
        //                 }
        //                 options.type = type
        //                 return overrideMessage(options)
        //             }
        //         })

        //         Vue.prototype.$message = overrideMessage
        //     }
        // }, 100)
    },
}

export { loading }
